/*
 * Copyright (c) 2014-2019 2018 xmars All Rights Reserved.
 * ProjectName: xmars
 * FileName: ErrorUtil.java
 * Author: YuZheng
 * Date: 18-12-13 下午6:51
 * Version: 1.0
 * LastModified: 18-12-5 下午2:29
 */

package com.topsun.framework.common.core.util;

import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;

/**
 * 异常相关工具类
 *
 * 
 */
public class ErrorUtil {
    private ErrorUtil() {
    }

    /**
     * 获取 errors 所有异常信息
     *
     * @param errors 错误集合
     * @return 所有错误信息
     */
    public static String getErrorsMessage(Errors errors) {
        StringBuilder result = new StringBuilder();
        for (ObjectError error : errors.getAllErrors()) {
            result.append(error.getDefaultMessage());
            result.append("\n");
        }
        return result.toString();
    }

    public static String getI18nErrorsMessage(I18nMessageUtil messageUtil, Errors errors){
        StringBuilder result = new StringBuilder();
        for (ObjectError error : errors.getAllErrors()) {
            result.append(messageUtil.getMessage(error.getDefaultMessage(), null));
            result.append("\n");
        }
        return result.toString();
    }
}
